package com.mcptt.config.ue.initial.sub;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.jdom.Element;
import org.jdom.JDOMException;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.product.log.Log;
import com.mcptt.config.ue.initial.sub.sub.ImeiRange;

public class McpttUeId {
	public static final String Name = "mcptt-UE-id";
	private static final String Name_InstanceIdUrn = "Instance-ID-URN";
	private static final String Name_Index = "index";

	private int index = 0;
	private List<String> instanceIdUrnList = null;
	private ImeiRange imeiRange = null;

	public McpttUeId()
	{
		instanceIdUrnList = new Vector<String>();
	}
	public void parse(Element elem) throws JDOMException
	{
		if(elem == null)
		{
			return;
		}
		String value = elem.getAttributeValue(Name_Index);
		index = CheckData.parseInt(value);
		
		List<Element> list = elem.getChildren();//��ø��ڵ���ӽڵ�  
		for(Element subElem:list) 
		{
			String name = subElem.getName();
			Log.debug(this.getClass(), "name = " + name);
			if(name == null
					|| name.length() <= 0)
			{
				return;
			}
			if(name.equals(Name_InstanceIdUrn))
			{
				name = subElem.getText();
				instanceIdUrnList.add(name);
			}
			else if(name.equals(ImeiRange.Name))
			{
				imeiRange = new ImeiRange();
				imeiRange.parse(subElem);
			}
		}  
	}

	public String toString()
	{
		StringBuffer strBuf = new StringBuffer("\r\n");
		strBuf.append("<").append(Name).append(" ").append(Name_Index).append("=\"").append(index).append("\">").append("\r\n");
		for(Iterator<String> itor = instanceIdUrnList.iterator();itor.hasNext();)
		{
			strBuf.append("    ").append("<").append(Name_InstanceIdUrn).append(">");
			strBuf.append(itor.next());
			strBuf.append("</").append(Name_InstanceIdUrn).append(">").append("\r\n");
		}
		strBuf.append(imeiRange);
		strBuf.append("</").append(Name).append(">").append("\r\n");

		return strBuf.toString();
	}
}
